package com.cloud.pole.shop.service.goods.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsPromotionBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsPromotion;
import com.cloud.pole.shop.model.entity.shops.ShopShopsComponent;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsPromotionAdminVo;
import com.cloud.pole.shop.service.goods.GoodsPromotionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

/*
*2019年7月4日
*cloud-shop
*author:frank-chen
*/
@Service
public class GoodsPromotionServiceImpl implements GoodsPromotionService{
	@Autowired 
	private ShopGoodsPromotionBiz promotionBiz;
	@Autowired
	private ShopGoodsBiz shopGoodsBiz;
	
	@Override
	public PageInfo<ShopGoodsPromotionAdminVo> selectShopGoodsPromotionAdminVoListByMap(String token, Map<String, Object> map,
			Integer pageNo, Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		int userType = jwtInfo.getType();
		//商家管理
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			if(StringUtils.isEmpty(jwtInfo.getAttr1())) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "merchantId为空");
			}
			map.put("merchantId", Integer.parseInt(jwtInfo.getAttr1()));
		}
		PageHelper.startPage(pageNo, pageSize); 
    	List<ShopGoodsPromotionAdminVo> list = promotionBiz.selectShopGoodsPromotionAdminVoListByMap(map);
    	PageInfo<ShopGoodsPromotionAdminVo> page = new PageInfo<ShopGoodsPromotionAdminVo>(list);
    	return page;
	}

	@Override
	public void deletePromotionBatch(String token, String promotionIds) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		int userType = jwtInfo.getType();
		Integer merchantId = null;
		//商家管理
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			if(StringUtils.isEmpty(jwtInfo.getAttr1())) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "merchantId为空");
			}
			merchantId = Integer.parseInt(jwtInfo.getAttr1());
		}
		String[] ids = promotionIds.split(",");
		if(ids.length <= 0) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "promotionIds错误");
		}
		for(int i = 0; i < ids.length; i ++) {
			ShopGoodsPromotion entity = promotionBiz.selectById(Integer.parseInt(ids[i]));
			if(entity != null) {
				if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
					//权限控制
					if(entity.getMerchantId() != null && entity.getMerchantId().intValue() != merchantId.intValue()) {
						throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "无权限操作");
					}
				}
				promotionBiz.deleteShopGoodsPromotion(entity);
			}
		}
	}

	@Override
	public void deletePromotion(String token, Integer id) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		int userType = jwtInfo.getType();
		Integer merchantId = null;
		//商家管理
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			if(StringUtils.isEmpty(jwtInfo.getAttr1())) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "merchantId为空");
			}
			merchantId = Integer.parseInt(jwtInfo.getAttr1());
		}
		ShopGoodsPromotion entity = promotionBiz.selectById(id);
		if(entity == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "推荐不存在");
		}
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			//权限控制
			if(entity.getMerchantId() != null && entity.getMerchantId().intValue() != merchantId.intValue()) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "无权限操作");
			}
		}
		promotionBiz.deleteShopGoodsPromotion(entity);
	}

	@Override
	public void addPromotion(String token, ShopGoodsPromotion entity) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		int userType = jwtInfo.getType();
		Integer merchantId = null;
		//商家管理
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			if(StringUtils.isEmpty(jwtInfo.getAttr1())) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "merchantId为空");
			}
			merchantId = Integer.parseInt(jwtInfo.getAttr1());
		}
//		log.info("getGoodsId:" + entity.getGoodsId() );
//		log.info("PromotionType:" + entity.getPromotionType() );    		
		//验证是否重复
		 Example example = new Example(ShopGoodsPromotion.class);
 	     example.createCriteria().andEqualTo("goodsId", entity.getGoodsId());
 	     example.createCriteria().andEqualTo("promotionType", entity.getPromotionType());
 	     List<ShopGoodsPromotion> list =  promotionBiz.selectByExample(example);
		 if(list != null && list.size() > 0) {
			 throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品已经被推荐，不用重复操作！");
		 }
		 //查询产品信息
		 ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(entity.getGoodsId());
		 //查询店铺信息
		 if(shopGoods == null) {
			 throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效！");
		 }
		 entity.setShopId(shopGoods.getShopId());
		 entity.setMerchantId(shopGoods.getMerchantId());
		 entity.setCreateTime(new Date());
		 promotionBiz.insertSelective(entity);
	}

	@Override
	public void modifyPromotion(String token, ShopGoodsPromotion entity) throws Exception {
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		int userType = jwtInfo.getType();
		Integer merchantId = null;
		//商家管理
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			if(StringUtils.isEmpty(jwtInfo.getAttr1())) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "merchantId为空");
			}
			merchantId = Integer.parseInt(jwtInfo.getAttr1());
		}
		//查询
		ShopGoodsPromotion searchPromotion = promotionBiz.selectById(entity.getPromotionId());
		if(searchPromotion == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "推荐不存在");
		}
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			//权限控制
			if(searchPromotion.getMerchantId() != null && searchPromotion.getMerchantId().intValue() != merchantId.intValue()) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "无权限操作");
			}
		}
		//验证是否重复
		 Example example = new Example(ShopGoodsPromotion.class);
 	     example.createCriteria().andEqualTo("goodsId", entity.getGoodsId());
 	     example.createCriteria().andEqualTo("promotionType", entity.getPromotionType());
 	     List<ShopGoodsPromotion> list =  promotionBiz.selectByExample(example);
		 if(list != null && list.size() > 0) {
			 ShopGoodsPromotion promotion = list.get(0);
			 if(promotion.getPromotionId().intValue() != entity.getPromotionId()) {
				 throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品已经被推荐，不用重复操作！");
			 }
		 }
//		 //查询产品信息
//		 ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(entity.getGoodsId());
//		 //查询店铺信息
//		 if(shopGoods == null) {
// 			return ApiRestResponse.fail("产品无效！", false);
//		 }
		 promotionBiz.updateShopGoodsPromotion(entity);
	}

	@Override
	public PageInfo<ShopGoodsPromotion> selectPageShopAdminList(Integer shopId, Integer componentId, Integer pageNo,
			Integer pageSize) throws Exception {
		PageHelper.startPage(pageNo, pageSize);
		List<ShopGoodsPromotion> selectPageShopAdminList =promotionBiz.selectPageShopAdminList(componentId,shopId);
		PageInfo<ShopGoodsPromotion> page = new PageInfo<ShopGoodsPromotion>(selectPageShopAdminList);
		return page;
	
	}

	@Override
	public ShopGoodsPromotion selectById(Integer shopId, Integer indexRecommNum, Integer promotionId) throws Exception {
		
		return promotionBiz.selectById(shopId, indexRecommNum, promotionId);
	}

	@Override
	public void updateShopGoodsPromotion(ShopGoodsPromotion shopGoodsPromotion) throws Exception {
			
		promotionBiz.updateOpenShopGoodsPromotion(shopGoodsPromotion);
	}
}
